package com.zkas.munqr;

import com.zkas.cuit.CuitReader;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.TextView;

public class CuitDialogFragment extends DialogFragment {
	private String cuit;
	private Handler handler;
	private AlertDialog errorDialog;
	private Builder errorDialogBuilder;
	private Dialog cuitDialog;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(R.layout.dialog_cuit, null);
		final TextView textCuit = (TextView) view.findViewById(R.id.text_cuit);
		textCuit.setText(cuit);

		builder.setView(view)
				// Add action buttons
				.setPositiveButton(R.string.button_ok,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								String cuit = textCuit.getText().toString();
								
								if (CuitReader.validar(cuit)) {
									Intent intent = new Intent(getActivity(), DisplayMessageActivity.class);
									intent.putExtra(DisplayMessageActivity.PARAM_CUIT, cuit);
									startActivity(intent);
								} 
								else {
									if (handler != null) {
										Message message = Message.obtain(handler, R.id.restart_preview);
										message.sendToTarget();									
									}
									
									errorDialog = errorDialogBuilder.create();
									errorDialog.setButton("OK", new DialogInterface.OnClickListener() {
									    @Override
									    public void onClick(DialogInterface dialog, int which) {
									    	errorDialog.dismiss(); 
									    	cuitDialog.show();
									    }
									});
									errorDialog.show();
								}
							}
						})
				.setNegativeButton(R.string.button_cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								if (handler != null) {
									Message message = Message.obtain(handler, R.id.restart_preview);
									message.sendToTarget();									
								}
								cuitDialog.cancel();
							}
						});
		
		cuitDialog = builder.create();
		
		errorDialogBuilder = new AlertDialog.Builder(cuitDialog.getContext());
		errorDialogBuilder.setCancelable(false); // This blocks the 'BACK' button
		errorDialogBuilder.setMessage("Cuit Incorrecto");
		

		return cuitDialog;
	}

	public void setCuit(String cuit) {
		this.cuit = cuit;
	}

	public void setActivityHandler(Handler handler) {
		this.handler = handler;
	}
}
